// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: implementation of COnUnloadHandler.
//   $Id$



// OnLoadHandler.cpp : Implementation of COnUnLoadHandler
#include "stdafx.h"
#include <new>
#include "XForms.h"
#include "OnLoadHandler.h"
#include "XFormsApp.h"
#include "eventmsg.h"

extern CXFormsApp theApp;

/////////////////////////////////////////////////////////////////////////////
// COnUnloadHandler
const CComBSTR sOnBeforeUnload(L"onbeforeunload");
const CComBSTR sOnUnload(L"onunload");
const CComBSTR sOnLoad(L"onload");
const CComBSTR sOnError(L"onerror");
COnUnloadHandler::COnUnloadHandler()
	:m_pXForms(0)
{
}

HRESULT COnUnloadHandler::Init(CXFormsXFormsElement * pForm, IHTMLWindow3 * pWin3)
{
	m_pXForms = pForm;
	if(m_pWin)
	{
		m_pWin->detachEvent(sOnUnload,this);
		m_pWin->detachEvent(sOnLoad,this);
		m_pWin->detachEvent(sOnError,this);
		m_pWin.Release();
	}

	m_pWin = pWin3;
	VARIANT_BOOL vtResult;
	m_pWin->attachEvent(sOnError,this,&vtResult);
	m_pWin->attachEvent(sOnLoad,this,&vtResult);
	return m_pWin->attachEvent(sOnUnload,this,&vtResult);
}


STDMETHODIMP COnUnloadHandler::Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pdispparams, VARIANT *pvarResult, EXCEPINFO *pexcepinfo,
		UINT *puArgErr)
{
#ifndef _DEBUG
	try {
#endif
		if (0 == dispidMember)
		{
			//Once the form has loaded, this should no longer respond to error
			//	as that can lead to a deletion occuring during event processing
			bool bOnload = false;
			if(pdispparams->cArgs > 0)
			{
				//Retrieve the event object
				CComVariant vt = pdispparams->rgvarg[0];
				if(vt.vt == VT_DISPATCH)
				{
					CComQIPtr<IHTMLEventObj> pEv(vt.pdispVal);
					//Check whether the event type is "load"
					CComBSTR sType;
					pEv->get_type(&sType);
					if(sType == &(sOnLoad[2]))
					{
						//If so, remove this as an onError handler, and prevent the 
						//	remaining function from deleting m_pXForms
						m_pWin->detachEvent(sOnError,this);
						bOnload = true;
					}
				}
			}

			if (!bOnload && m_pXForms)
			{
				m_pWin->detachEvent(sOnUnload,this);
				m_pWin->detachEvent(sOnLoad,this);
				m_pWin->detachEvent(sOnError,this);
				m_pWin.Release();
				delete m_pXForms;
				m_pXForms = 0;
			}
		}
#ifndef _DEBUG
	}
	catch(std::bad_alloc) {
		if(SUCCEEDED(theApp.FormsManager().RegisterEventLog()))
			theApp.FormsManager().WriteEvent(EVENTLOG_ERROR_TYPE, MSG_MEMEX);
		ASSERT(0);
	}
	catch(...) {
		if(SUCCEEDED(theApp.FormsManager().RegisterEventLog()))
			theApp.FormsManager().WriteEvent(EVENTLOG_ERROR_TYPE, MSG_TOPEX);
		ASSERT(0);
	}
#endif
	return S_OK;
}


